package com.lqkj.maptest.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Linm
 * @title
 * @date 2021/8/19 13:35
 */
public class SaveFile {

    /**
     * @param file 文件保存本地
     * @return OK
     */
     public static Object saveFile(MultipartFile file){
        if(file.isEmpty()){
            return "未选择文件";
        }
        //获取上传前文件的名字
        String fileName = file.getOriginalFilename();
        String filePath = "H://test/fileTmp/";

        File fileTmp = new File(filePath);
        boolean isDirectoryCreated = fileTmp.mkdirs();
        if (!isDirectoryCreated){
            isDirectoryCreated = fileTmp.mkdirs();
        }

        File localFile = new File(filePath+fileName);
        try {
            //上传的文件保存到本地
            file.transferTo(localFile);
            System.out.println(file.getOriginalFilename()+" 上传成功！");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }

        return "ok";
    }
}
